<template>
  <div>
    <!-- 补贴信息审核 -->
    <TopFilter :list="topFilterList" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110" @resetChange="resetChange"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTables
        ref="table"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        columnsRef="zjsubsidyAudit"
        :onRowSelectChange="selectChangeHandle"
        :onColumnsChange="columns => (this.columns = columns)"
      >
        <template slot="controls" slot-scope="props">
          <multiuse-button size="small" type="primary" @click="addHandle(1)">导 出</multiuse-button>
          <multiuse-button size="small" type="primary" @click="addHandle(2)" :disabled="isOpen">驳 回</multiuse-button>
        </template>
      </FilterTables>
    </Spin>

    <BaseDialog :visible.sync="issearch" :title="'经销商查询'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <ownerSeacher :rowData="formData" @close="ownerClose" />
    </BaseDialog>
    <Drawer :visible.sync="visible" :title="'详情'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px', minWidth: '650px' }" width="85%">
      <childrenModel :rowData="formData" @close="emitcloseHandler" />
    </Drawer>
    <BaseDialog :visible.sync="isreject" :title="'驳回意见'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <reject :list="selectedList" @close="rejectclose" />
    </BaseDialog>
  </div>
</template>
<script>
import { getList, getDetail, updatePlateApply, exportList,exporturl } from '@/api/omSale/subsidy/subsidyAudit/index.js';
import ownerSeacher from './ownerSeacher';
import childrenModel from './childrenModel';
import { dictionary } from '@/utils/dictMixin';
import { exportExcelFile } from '@/utils';
import reject from './reject';
export default {
  name: 'TemplateDistribution',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: getList,
      fetchParams: {
        applyStatus: 60191002,
        xhrAbort: true
      },
      formData: {},
      exportLoading: false,
      visible: false,
      issearch: false,
      isreject: false,
      isOpen: true,
      selectedList: []
    };
  },
  mixins: [dictionary],
  components: {
    ownerSeacher,
    childrenModel,
    reject
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {},
  methods: {
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'ownerCode',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun()}></el-button>
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请输入'
        },
        {
          type: 'TEXT_AREA',
          label: 'VIN',
          fieldName: 'vins',
          placeholder: '请输入',
          maxlength: '',
          maxRows: 2
        },
        {
          type: 'INPUT',
          label: '客户名称',
          fieldName: 'customerName',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '车牌号',
          fieldName: 'plateNumber',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '发票号码',
          fieldName: 'invoiceNo',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '上牌省份',
          fieldName: 'plateProvince',
          placeholder: '请选择',
          itemList: this.createRegionsList(),
          change: p => {
            if (p) {
              this.topFilterList.map(x => {
                if (x.fieldName == 'plateCity') {
                  x.initialValue = '';
                  x.itemList = this.createcityList(p);
                }
              });
            } else {
              this.topFilterList.map(x => {
                if (x.fieldName == 'plateCity') {
                  x.itemList = [];
                  x.initialValue = '';
                }
              });
            }
          }
        },
        {
          type: 'SELECT',
          label: '上牌城市',
          fieldName: 'plateCity',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'RANGE_DATE',
          label: '申请日期',
          fieldName: 'startApplyDate|endApplyDate',
          placeholder: '请选择',
          initialValue: []
        },
        {
          type: 'RANGE_DATE',
          label: '行驶证注册日期',
          fieldName: 'startRegisterDate|endRegisterDate',
          placeholder: '请选择',
          initialValue: []
        },
        {
          type: 'SELECT',
          label: '客户类型',
          fieldName: 'customerType',
          placeholder: '请选择',
          itemList: this.createDictList('1523')
        },

        {
          type: 'SELECT',
          label: '车价是否含国补',
          fieldName: 'includeNationalSubsidy',
          placeholder: '请选择',
          itemList: [
            { text: '是', value: 1 },
            { text: '否', value: 0 }
          ]
        },
        {
          type: 'INPUT',
          label: '公告号',
          fieldName: 'announceNo',
          placeholder: '请输入'
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 50,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row)}>
                  详情
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '经销商代码',
          dataIndex: 'ownerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户名称',
          dataIndex: 'customerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户类型',
          dataIndex: 'customerType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1523'),
          render: p => {
            return <span>{this.createDictText(p.row.customerType, '1523')}</span>;
          }
        },
        {
          title: '手机号',
          dataIndex: 'linkPhone',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '审批状态',
          dataIndex: 'applyStatus',
          sorter: true,
          render: p => {
            return <span>{this.createDictText(p.row.applyStatus, '6019')}</span>;
          }
        },
        {
          title: 'VIN码',
          dataIndex: 'vin',
          align: 'center',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '车系名称',
          dataIndex: 'seriesName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车型名称',
          dataIndex: 'modelName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配置名称',
          dataIndex: 'packageName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '选装名称',
          dataIndex: 'optionName',
          align: 'center',
          width: 250,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '内饰',
          dataIndex: 'trimName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '颜色',
          dataIndex: 'colorName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品代码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品名称',
          dataIndex: 'productName',
          align: 'center',
          width: 400,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '公告号',
          dataIndex: 'announceNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '开票日期',
          dataIndex: 'invoiceDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '发票号码',
          dataIndex: 'invoiceNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车辆性质',
          dataIndex: 'vehicleType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('4103'),
          render: p => {
            return <span>{this.createDictText(p.row.vehicleType, '4103')}</span>;
          }
        },
        {
          title: '证件类型',
          dataIndex: 'cardType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1508'),
          render: p => {
            return <span>{this.createDictText(p.row.cardType, '1508')}</span>;
          }
        },

        {
          title: '证件号码',
          dataIndex: 'cardNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '联系人',
          dataIndex: 'linkMan',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '省份',
          dataIndex: 'provinceName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '城市',
          dataIndex: 'cityName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '区县',
          dataIndex: 'districtName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '详细地址',
          dataIndex: 'linkAddress',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '邮编',
          dataIndex: 'zipCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '车牌号',
          dataIndex: 'plateNumber',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '行驶证注册日期',
          dataIndex: 'drivingRegisteDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '行驶证号',
          dataIndex: 'drivingNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '电机号',
          dataIndex: 'motorNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '电池号',
          dataIndex: 'batteryNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '销售顾问',
          dataIndex: 'salesManName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '申请日期',
          dataIndex: 'applyDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        }
      ];
    },
    selectChangeHandle(row) {
      if (row.length > 0) {
        let isOpen = true;
        let is = false;
        row.map(x => {
          if (x.applyStatus == 60191002) {
            isOpen = false;
          } else {
            is = true;
          }
        });
        this.isOpen = !isOpen && !is ? false : true;
        this.selectedList = row;
      } else {
        this.selectedList = [];
        this.isOpen = true;
      }
    },

    seacherFun() {
      this.issearch = true;
    },
  async  addHandle(type) {
      if (type == 1) {
        let json = Object.assign({}, { CODE: 'CODE_0000078' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
        // this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
        //   confirmButtonText: '下载',
        //   cancelButtonText: '取消',
        //   type: 'warning'
        // })
        //   .then(() => {
        //     let json = this.fetchParams;
        //     this.exportLoading = true;

        //     exportList(json)
        //       .then(response => {
        //         exportExcelFile(response.data, '补贴信息审核');
        //         this.exportLoading = false;
        //       })
        //       .catch(err => {
        //         console.log(err);
        //       });
        //   })
        //   .catch(() => {
        //     console.log('456');
        //     this.$message({
        //       type: 'info',
        //       message: '已取消'
        //     });
        //   });
      } else {
        this.isreject = true;
      }
    },
    changeHandle(params) {
      this.fetchParams.xhrAbort = false;
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    editHandle(data) {
      this.formData = data;
      this.visible = true;
    },
    ownerClose(val, data) {
      if (data) {
        let dealerCodes = [];
        data.map(x => {
          dealerCodes.push(x.dealerCode);
        });
        this.topFilterList.find(x => x.fieldName == 'ownerCode').initialValue = dealerCodes.toString();
      }
      this.issearch = false;
    },
    emitcloseHandler(val) {
      this.visible = false;
      if (val) {
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    },
    rejectclose(val) {
      this.isreject = false;
      if (val) {
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    },
    resetChange(val) {
      this.topFilterList.map(x => {
        if (x.fieldName == 'plateCity') {
          x.itemList = [];
          x.initialValue = '';
        }
      });
    }
  }
};
</script>
